package core;

<<<<<<< HEAD

import repository.Repository;
import fileoperation.FileCreation;
import fileoperation.FileDeletion;
=======
//import index.Index;
import repository.Repository;
import fileoperation.FileCreation;
import fileoperation.FileDeletion;
//import fileoperation.FileReader;
>>>>>>> 86693ae5299cdac0871be1578566d765f59b5e42
import gitobject.Commit;
import gitobject.GitObject;
import gitobject.Tree;

import java.io.File;
import java.io.FileWriter;
<<<<<<< HEAD
=======
import java.io.IOException;
>>>>>>> 86693ae5299cdac0871be1578566d765f59b5e42


public class JitReset {
    public static void reset(String mode, String commitId) throws Exception {

        //判断输入的这个commitId存不存在，通过判断id值是否为null
        String id = commitExists(commitId);

<<<<<<< HEAD
=======
        System.out.println("commitExists判断为存在，id是【" + id + "】");//测试用



>>>>>>> 86693ae5299cdac0871be1578566d765f59b5e42
        if("".equals(id)){
            System.out.println("没有名为" + commitId + "的commit");
        }
        else {
            if (mode.equals("--soft")){
                try {
                    softReset(id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (mode.equals("--mixed")){
                try {
                    mixedReset(id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (mode.equals("--hard")){
                try {
                    hardReset(id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


<<<<<<< HEAD
//检查commit是否存在，如果存在返回commitId，不存在返回null
public static String commitExists(String commitId) throws Exception {
    String lastCommit = new Commit().getLastCommit();
    if (lastCommit == null) {
        return "";
    }
    else {
        do {
            String lastId = lastCommit;
            if (lastId.equals(commitId)) {
                return lastCommit;
            }
            Commit currentCommit = Commit.deserialize(lastCommit);
            lastCommit = currentCommit.getParent();
        }
        while (!"".equals(lastCommit));
    }
    return "";
}

//--soft: 仅仅将HEAD指针指向指定的commit，其他不变
public static void softReset(String commitId) throws Exception {
    File HEAD = new File(Repository.getGitDir() + File.separator + "HEAD");
    String path = GitObject.getValue(HEAD).replace("\n", "").substring(5);

    File branchFile = new File(Repository.getGitDir() + File.separator + path);
    FileWriter fw = new FileWriter(branchFile, false);
    fw.write(commitId);
    fw.close();
    System.out.println("soft reset成功了!");
}

//--mixed: 重置暂存区索引，但是不重置工作区。（更改的文件被保留，但是会被标记为未提交）。这是默认的模式。
public static void mixedReset(String commitId) throws Exception {
    // 先把HEAD指向commit
    softReset(commitId);
    // 再删除index里的文件
    File indexFile = new File(Repository.getGitDir() + File.separator + "index");
    FileDeletion.deleteContent(indexFile);
    System.out.println("mixed reset成功了!");
}


//--hard:  同时重置索引与工作区文件，并且此commit之后的所有commit信息将会丢失。
public static void hardReset(String commitId) throws  Exception {
    //先把HEAD指向commit，删除index里的文件
    mixedReset(commitId);
    //再把工作区的文件重置成commit
    Commit commit = Commit.deserialize(commitId);
    String treeKey = commit.getTree();

    Tree tree = Tree.deserialize(treeKey);
    FileCreation.recoverWorkTree(tree, Repository.getWorkTree());
    System.out.println("hard reset成功了!");
=======

    //检查commit是否存在，如果存在返回commitId，不存在返回null
    public static String commitExists(String commitId) throws Exception {
        String lastCommit = new Commit().getLastCommit();
        if (lastCommit == null) {
            return "";
        }
        else {
            do {
                String lastId = lastCommit;
                if (lastId.equals(commitId)) {
                    return lastCommit;
                }
                Commit currentCommit = Commit.deserialize(lastCommit);
                lastCommit = currentCommit.getParent();
            }
            while (!"".equals(lastCommit));
        }
        return "";
    }

    //--soft: 仅仅将HEAD指针指向指定的commit，其他不变
    public static void softReset(String commitId) throws Exception {
        File HEAD = new File(Repository.getGitDir() + File.separator + "HEAD");
        String path = GitObject.getValue(HEAD).replace("\n", "").substring(5);
        //String path2 = GitObject.getValue(HEAD);//测试用
        System.out.println("path是"+path);//测试用
        //System.out.println("path2是"+path2);//测试用

        File branchFile = new File(Repository.getGitDir() + File.separator + path);
        FileWriter fw = new FileWriter(branchFile, false);
        fw.write(commitId);
        fw.close();
        System.out.println("soft reset成功了!");
    }

    //--mixed: 重置暂存区索引，但是不重置工作区。（更改的文件被保留，但是会被标记为未提交）。这是默认的模式。
    public static void mixedReset(String commitId) throws Exception {
        // 先把HEAD指向commit
        softReset(commitId);
        // 再删除index里的文件
        File indexFile = new File(Repository.getGitDir() + File.separator + "index");
        FileDeletion.deleteContent(indexFile);
        System.out.println("mixed reset成功了!");
    }


    //--hard:  同时重置索引与工作区文件，并且此commit之后的所有commit信息将会丢失。
    public static void hardReset(String commitId) throws  Exception {
        //先把HEAD指向commit，删除index里的文件
        mixedReset(commitId);
        //再把工作区的文件重置成commit
        Commit commit = Commit.deserialize(commitId);
        String treeKey = commit.getTree();

        System.out.println("treekey是" + treeKey);//测试用

        Tree tree = Tree.deserialize(treeKey);
        FileCreation.recoverWorkTree(tree, Repository.getWorkTree());
        System.out.println("hard reset成功了!");
>>>>>>> 86693ae5299cdac0871be1578566d765f59b5e42
    }
}





















